﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;
namespace project
{
    public partial class FormSignUp : UIForm
    {
        public FormSignUp()
        {
            InitializeComponent();
        }

        private void FormSignUp_Load(object sender, EventArgs e)
        {

        }

        private void uiLabel1_Click(object sender, EventArgs e)
        {

        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            // 注册确认
            string num = uiTextBox1.Text;
            string pwd = uiTextBox3.Text;
            string pwd2 = uiTextBox2.Text;
            string name = uiTextBox4.Text;
            string phone_num = uiTextBox5.Text;
            if(string.IsNullOrEmpty(num)||string.IsNullOrEmpty(pwd2)
                || string.IsNullOrEmpty(pwd) || string.IsNullOrEmpty(name) || string.IsNullOrEmpty(phone_num))
            {
                MessageBox.Show("请填写所有信息", "提示", MessageBoxButtons.OK,MessageBoxIcon.Warning);
            }
            else if (!pwd2.Equals (pwd))
            {
                MessageBox.Show("两次密码输入不一致", "提示", MessageBoxButtons.OK,MessageBoxIcon.Warning);
            }
            else if((!StringHelper.IsNumeric(num) ) || (!StringHelper.IsNumeric(phone_num)))
            {
                MessageBox.Show("学号或者手机号存在非法字符", "提示", MessageBoxButtons.OK,MessageBoxIcon.Warning);

            }
            else // 基本输入正确
            {
                string sql = "select count(*) from student where number=" + num
                    + " ;";
                object num_obj = SqlHelper.ExecuteScalar(sql) ;

                if (num_obj == null)
                {
                    MessageBox.Show("数据库访问出错", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else if((long)num_obj == 1)
                {
                    MessageBox.Show("该学号已经注册过", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                }
                else // 注册账号
                {
                    sql = "insert into student(number,pwd,name,phone_number) values("
                        + num + "," + pwd + "," + name + "," + phone_num + ");";
                    SqlHelper.ExecuteNonQuery(sql);
                    MessageBox.Show("注册成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                }
            }
        }

        private void uiTextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void uiButton2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
